package cn.schoolwow.util.module.query.jsonarray.flow.get;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.module.query.jsonarray.domain.JSONArrayQueryOption;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetJSONArraySingleFieldValueListBusiness implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONArrayQueryOption jsonQueryOption = flowContext.checkInstanceData(JSONArrayQueryOption.class);
        JSONArray filterArray = (JSONArray) flowContext.getData("filterArray");
        String fieldName = (String) flowContext.checkData("fieldName");

        if(null==filterArray){
            return;
        }
        List fieldValueList = new ArrayList();
        for(int i=0;i<filterArray.size();i++){
            JSONObject object = filterArray.getJSONObject(i);
            Object fieldValue = object.get(fieldName);
            if(null==fieldValue){
                continue;
            }
            if(jsonQueryOption.distinct&&fieldValueList.contains(fieldValue)){
                continue;
            }
            fieldValueList.add(fieldValue);
        }
        flowContext.putReturnData("fieldValueList", fieldValueList);
    }

    @Override
    public String name() {
        return "获取JSON数组单个字段值列表";
    }
}